package com.knight.hadoop.day05.simulate.hdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

public class FileUtils {

	/**
	 * 缓冲区大小
	 */
	private static final int BUFFER_LENGTH = 1024 * 10;

	/**
	 * 存储文件存储路径
	 */
	private static final String CONFIG_PATH = "D://config.properties";

	/**
	 * 切割文件保存路径
	 */
	private static final String SPICT_DIRECTORY = "D:\\hdfs\\block\\";
	
	
	/**
	 * 上传文件的方法
	 * @param is
	 * @param fileName
	 * @throws IOException
	 */
	public static void upload(InputStream is,String fileName) throws IOException {
		// 源文件输入流
		InputStream inputStream = null;
		// 保存切分后的配置文件
		OutputStream configOps = null;
		try {
				inputStream = is;

				// 保存文件存储的目录
				Properties proInfo = new Properties();
				configOps = new FileOutputStream(CONFIG_PATH);

				// 2、通过outputStream将input流发送给服务器
				int length = 0;
				int count = 1;// 切割后每个文件的名称
				StringBuilder sb = new StringBuilder();
				byte[] buffer = new byte[BUFFER_LENGTH]; // 1M

				while ((length = inputStream.read(buffer, 0, BUFFER_LENGTH)) != -1) {
					String filePath = SPICT_DIRECTORY + count;
					// 输出文件
					OutputStream out = new FileOutputStream(filePath);
					out.write(buffer, 0, length);
					out.close();
					count++;
					sb.append(filePath + ",");
				}

				String filePaths = sb.toString();

				proInfo.setProperty(fileName, filePaths.substring(0, filePaths.length() - 1));
				proInfo.store(configOps, "config");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
			if (configOps != null) {
				configOps.close();
			}
		}
	}

	/**
	 * 上传方法
	 * 
	 * @throws IOException
	 */
	public static void upload(File file) throws IOException {
		upload(new FileInputStream(file),file.getName());
	}

	/**
	 * 下载方法
	 * 
	 * @throws IOException
	 */
	public static void download(String fileName) throws IOException {

		InputStream ips = null;
		OutputStream out = null;
		try {
			ips = new FileInputStream(CONFIG_PATH);
			Properties config = new Properties();
			config.load(ips);

			Set keySet = config.keySet();
			Iterator iterString = keySet.iterator();

			String filePath = "";

			while (iterString.hasNext()) {
				String tmpFileName = (String) iterString.next();
				if (fileName.equals(tmpFileName)) {
					// 找到下载的文件路径，格式是：d\://a1,d\://a2
					filePath = config.getProperty(tmpFileName);
					break;
				}
			}
			List<String> filePaths = new ArrayList<>();
			if (filePath.contains(",")) {
				filePaths = Arrays.asList(filePath.split(","));
			} else {
				filePaths.add(filePath);
			}

			if (filePaths.size() > 0) {
				out = new FileOutputStream("D://" + fileName);
				for (String sourcePath : filePaths) {

					FileInputStream sourceFile = new FileInputStream(sourcePath);

					byte[] buffer = new byte[BUFFER_LENGTH];// 缓冲区文件大小为1M
					int len = 0;
					while ((len = sourceFile.read(buffer, 0, BUFFER_LENGTH)) != -1) {
						out.write(buffer, 0, len);
					}
					sourceFile.close();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ips != null) {
				ips.close();
			}
			if (out != null) {
				out.close();
			}
		}
		/*
		 * FileInputStream i1 = new FileInputStream("d://a1"); FileInputStream
		 * i2 = new FileInputStream("d://a2");
		 * 
		 * String filePath = "d://1.ext"; OutputStream out = new
		 * FileOutputStream(filePath); byte[] buffer = new byte[1024 * 1024 *
		 * 1];// 缓冲区文件大小为1M int len = 0; while ((len = i1.read(buffer, 0,
		 * BUFFER_LENGTH)) != -1) { out.write(buffer, 0, len); } i1.close(); len
		 * = 0; while ((len = i2.read(buffer, 0, BUFFER_LENGTH)) != -1) {
		 * out.write(buffer, 0, len); } i2.close(); out.close();
		 */

	}
	
	/**
	 * 下载方法
	 * 
	 * @throws IOException
	 */
	public static void downloadSocket(Socket socket,String fileName) throws IOException {

		InputStream ips = null;
		OutputStream out = null;
		try {
			ips = new FileInputStream(CONFIG_PATH);
			Properties config = new Properties();
			config.load(ips);

			Set keySet = config.keySet();
			Iterator iterString = keySet.iterator();

			String filePath = "";

			while (iterString.hasNext()) {
				String tmpFileName = (String) iterString.next();
				if (fileName.equals(tmpFileName)) {
					// 找到下载的文件路径，格式是：d\://a1,d\://a2
					filePath = config.getProperty(tmpFileName);
					break;
				}
			}
			List<String> filePaths = new ArrayList<>();
			if (filePath.contains(",")) {
				filePaths = Arrays.asList(filePath.split(","));
			} else {
				filePaths.add(filePath);
			}

			if (filePaths.size() > 0) {
				out = socket.getOutputStream();
				for (String sourcePath : filePaths) {

					FileInputStream sourceFile = new FileInputStream(sourcePath);

					byte[] buffer = new byte[BUFFER_LENGTH];// 缓冲区文件大小为1M
					int len = 0;
					while ((len = sourceFile.read(buffer, 0, BUFFER_LENGTH)) != -1) {
						out.write(buffer, 0, len);
					}
					sourceFile.close();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ips != null) {
				ips.close();
			}
			if (out != null) {
				out.close();
			}
		}
		/*
		 * FileInputStream i1 = new FileInputStream("d://a1"); FileInputStream
		 * i2 = new FileInputStream("d://a2");
		 * 
		 * String filePath = "d://1.ext"; OutputStream out = new
		 * FileOutputStream(filePath); byte[] buffer = new byte[1024 * 1024 *
		 * 1];// 缓冲区文件大小为1M int len = 0; while ((len = i1.read(buffer, 0,
		 * BUFFER_LENGTH)) != -1) { out.write(buffer, 0, len); } i1.close(); len
		 * = 0; while ((len = i2.read(buffer, 0, BUFFER_LENGTH)) != -1) {
		 * out.write(buffer, 0, len); } i2.close(); out.close();
		 */

	}
}
